//
//  SRSetTargetView.m
//  SportRing
//
//  Created by icelee on 14-10-28.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRSetTargetView.h"
#import "SRMe+Cate.h"

@implementation SRSetTargetViewModel

@end

@interface SRSetTargetView()<UITextFieldDelegate>

@property (nonatomic, weak)IBOutlet UITextField *stepsTextField;
@property (nonatomic, weak)IBOutlet UILabel *distanceLabel;
@property (nonatomic, weak)IBOutlet UILabel *kalorieLabel;
@property (nonatomic, weak)IBOutlet UIButton *okButton;

@end

@implementation SRSetTargetView

- (void)awakeFromNib{
    [super awakeFromNib];
    [self calValues];
    [self.stepsTextField addTarget:self action:@selector(textChangeAction:) forControlEvents:UIControlEventEditingChanged];

}

- (IBAction)okButtonAction:(id)sender
{
    [self calValues];
    
    [self fadeOut];
    
    if (_srTargetDelegate && [_srTargetDelegate respondsToSelector:@selector(setTargetSteps:)])
    {
        [_srTargetDelegate setTargetSteps:self];
        
    }
    
}

- (void)calValues
{
    float distance = [SRHelper distanceMeterWithHeightMeter:[[SRMe currentUser].height floatValue]/100.0 steps:[self.stepsTextField.text floatValue]];
    int kalorie = [SRHelper caloriesWithHeightMeter:[[SRMe currentUser].height floatValue]/100.0 weight:[[SRMe currentUser].weight floatValue] steps:[self.stepsTextField.text floatValue]];
        
    self.distanceLabel.text = [NSString stringWithFormat:@"%.1f千米",distance];
    self.kalorieLabel.text = [NSString stringWithFormat:@"%d卡路里",kalorie];
    
    _setTargetViewModel = [[SRSetTargetViewModel alloc] init];
    _setTargetViewModel.steps = [self.stepsTextField.text floatValue];
    _setTargetViewModel.distance = distance;
    _setTargetViewModel.calorie = kalorie;
}

#pragma mark - additional function
+ (SRSetTargetView*)LoadSRSetTargetView
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SRSetTargetView" owner:self options:nil];

    return [nib objectAtIndex:0];
}

- (void)textChangeAction:(id)sender
{
    [self calValues];
}

@end
